package com.hiray.taglayoutmanager;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hiray.taglayoutmanager.uitility.ClickableViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by hiray on 2017/12/15.
 *
 * @author hiray
 */

public class TagAdapter extends RecyclerView.Adapter<TagAdapter.ViewHolder> {


    private List<Integer> tintColor = Arrays.asList(
            R.drawable.tag_drawable_0,
            R.drawable.tag_drawable_1,
            R.drawable.tag_drawable_2,
            R.drawable.tag_drawable_3,
            R.drawable.tag_drawable_4,
            R.drawable.tag_drawable_5,
            R.drawable.tag_drawable_6,
            R.drawable.tag_drawable_7,
            R.drawable.tag_drawable_8
    );


    private List<String> source = Arrays.asList(
            "浏览器", "视频",
            "音乐", "唯美",
            "小清新", "可爱",
            "女生 性感", "鲨鱼大树",
            "风景 爱情", "樱桃小丸子",
            "高清手机壁纸", "个性清纯", "....是怎么样的体验?",
            "鲜花 写真", "美食 静物",
            "旅游", "帝都雾霾最为致命",
            "东北大野猪", "演员的诞生真的low!", "知乎变捞了 ", "Telegram",
            "Rxunfurl", "Apache", "WhiteHouse", "trump", "HotFix", "Bilibili",
            "AcFun", "Sakura", "ハーフムーン・セレナーデ", "桜の暗に振り向けば",
            "Faye Wong", "我有一段rap不知当讲不当讲", "Miao"
    );
    private List<String> tags = new ArrayList<>(source);
    private Context context;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null)
            context = parent.getContext();
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        return new ViewHolder(layoutInflater.inflate(
                R.layout.tag_item, parent, false
        ));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.textView.setText(tags.get(holder.getAdapterPosition()));
        holder.textView.setBackgroundResource(tintColor.get(position % tintColor.size()));
    }

    @Override
    public int getItemCount() {
        return tags.size();
    }

    public class ViewHolder extends ClickableViewHolder {
        AppCompatTextView textView;

         ViewHolder(View itemView) {
            super(itemView);
            textView = (AppCompatTextView) itemView;
        }

        @Override
        public void onClick(View v) {
            int adapterPosition = getAdapterPosition();
            tags.remove(adapterPosition);
            notifyItemRemoved(adapterPosition);
        }
    }


}
